#ifndef SERIALMANAGER_H
#define SERIALMANAGER_H

#include <QObject>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QComboBox>
#include <QDateTime>

struct SerialParams {
    QString portName;
    int baudRate;
    int dataBits;
    QString parity;
    QString stopBits;
};

class SerialManager : public QObject
{
    Q_OBJECT

public:
    explicit SerialManager(QObject *parent = nullptr);
    ~SerialManager();

    void autoFillSerialPorts(QComboBox *comboBox);
    bool openSerialPort(const SerialParams &params);
    void closeSerialPort();
    qint64 writeData(const QByteArray &data);
    QString lastError() const;
    QString currentPortName() const;
    bool isOpen() const;

signals:
    void dataReady(const QByteArray &data);
    void connectionStatusChanged(bool connected, const QDateTime &timestamp);
    void errorOccurred(const QString &error);

private slots:
    void onReadyRead();
    void handleError(QSerialPort::SerialPortError error);

private:
    QSerialPort *serialPort_;
    QString lastError_;
};

#endif // SERIALMANAGER_H
